package com.lxq.live.controller;

import com.lxq.live.dao.MongoTestDao;
import com.lxq.live.pojo.MongoDBTestPoJo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;


@Slf4j
@RestController
@Api(tags = "开发测试接口")
public class HelloController {

    @Autowired
    MessageSource messageSource;
    @Autowired
    MongoTestDao mongoTestDao;
    @Value("${spring.datasource.url}")
    private String mysqlUrl;

    @GetMapping("/admin/test")
    public String adminSuccess() {
        return "<h1>Admin登录成功</h1>";
    }

    @GetMapping("/user/test")
    public String userSuccess() {
        return "<h1>user登录成功</h1>";
    }

    @GetMapping("/international_greet")
    public String internationalGreet() {
        return messageSource.getMessage("greet", null, LocaleContextHolder.getLocale());
    }

    @GetMapping("/advice_testing")
    public String adviceTesting(Model model) {
        var modMap = model.asMap();
        var covertMap = (HashMap<String, Object>) modMap.get("serviceAttitude");
        return String.format("%s Service Health:%s %s", "<h1>", covertMap.get("health"), "</h1>");
    }

    @GetMapping("/testing/get_mysqlurl")
    public HashMap<String, String> getMysqlUrl() {
        var result = new HashMap<String, String>();
        result.put("mysqlUrl", mysqlUrl);
        return result;
    }

    @GetMapping("/path_interceptor_testing/testing")
    public String pathInterceptorTesting() {
        return "拦截器测试";
    }

    @GetMapping("/mongotest/insert")
    public MongoDBTestPoJo mongoInsert(MongoDBTestPoJo poJo) {
        mongoTestDao.insert(poJo);
        return mongoTestDao.findById(poJo.getId()).orElseThrow();
    }

    @GetMapping("/mongotest/find")
    public MongoDBTestPoJo mongoFind(int id) {
        return mongoTestDao.findById(id).orElseThrow();
    }
}
